import { ILoadMapTypeAll } from '@/common';
import { getPosIDs, getTenantID, getUserID } from '@/utils';
import { SHTG_CHECKSTATE, SHTH_CHECKSTATE } from '../../Acommon';
import { CQXG_STATE, DB_STATE, LB_STATE, YDB_STATE } from '../common/Format';
import { message } from 'antd';

const AllBtnsConfig = (curRes: Record<string, any>, detailInfo: Record<string, any>, flowType: string, currendKey: string) => {
  let isShowBtn = {
    allBtnDiv: false,
    revokeFlagBtn: false, //撤销按钮
    invalBoolBtn: false, //暂停开始按钮
    stopProcInstBtn: false, //流程作废接口
    applyBtnDiv: false, //提交按钮
    flowHandList: {}, //流程办理数据
  };

  /** 流程按钮 */
  if (detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll?.CONTRACT_STATE}1001`) {
    isShowBtn['revokeFlagBtn'] = true;
    isShowBtn['stopProcInstBtn'] = true;
  }

  /**澄清提交流程按钮判断*/
  if (
    curRes?.['/purchaseSourceClarifyController/startProcess/{starterDeptID}'] &&
    detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll.PROCURECLARFY_STATE}1000` &&
    !detailInfo?.checkState &&
    currendKey == 'source-clarify'
  ) {
    isShowBtn['allBtnDiv'] = true;
    isShowBtn['applyBtnDiv'] = true;
  }

  /**定标提交流程按钮判断*/
  if (
    detailInfo?.state == `${getTenantID()}${ILoadMapTypeAll?.CALIBRATE_STATE}1000` &&
    !detailInfo?.checkState &&
    currendKey == 'source-calibrate' &&
    curRes?.['/purchaseSourceCalibrateController/startProcess/{starterDeptID}']
  ) {
    isShowBtn['allBtnDiv'] = true;
    isShowBtn['applyBtnDiv'] = true;
  }

  const taskHandInfo: Record<string, any> = detailInfo?.paramObject?.[flowType];

  if (
    taskHandInfo &&
    taskHandInfo?.mactivitiTaskList?.length > 0 &&
    taskHandInfo?.mactivitiTaskList?.[0]?.assigneePositionID == getPosIDs() &&
    taskHandInfo?.mactivitiTaskList?.[0]?.assigneeID == getUserID()
  ) {
    isShowBtn['invalBoolBtn'] = true;
    isShowBtn['allBtnDiv'] = true;
    // isShowBtn['flowHandList'] = taskHandInfo?.mactivitiTaskList?.[0];
  }
  isShowBtn['flowHandList'] = detailInfo;

  return isShowBtn;
};

/** 公告按钮权限 */
const updateInviteBtn = (curRes: Record<string, any>, dataInfo: Record<string, any>) => {
  let isShowBtn = {
    allBtnDiv: false,
    inviteSaveBtn: false,
    applyBtn: false,
  };
  if (dataInfo?.id && dataInfo?.state && dataInfo?.bidClearTime) {
    if (curRes?.['/purchaseSourceController/editBid'] && !dataInfo?.noticeCode && dataInfo?.state == `${getTenantID()}${ILoadMapTypeAll.CURRYOUT_STATE}1010`) {
      isShowBtn['allBtnDiv'] = true;
      isShowBtn['inviteSaveBtn'] = true;
    }
    if (curRes?.['/purchaseSourceController/editState'] && dataInfo?.state == `${getTenantID()}${ILoadMapTypeAll.CURRYOUT_STATE}1010`) {
      isShowBtn['allBtnDiv'] = true;
      isShowBtn['applyBtn'] = true;
    }
  } else {
    if (curRes?.['/purchaseSourceController/addBid']) {
      isShowBtn['allBtnDiv'] = true;
      isShowBtn['inviteSaveBtn'] = true;
    }
  }

  return isShowBtn;
};

/** 采购澄清按钮权限 （curRes:权限, dataInfo: 当前澄清详情, packListInfo: 当前选中标包） */
const updateClarifyBtn = (curRes: Record<string, any>, dataInfo: Record<string, any>, packListInfo: any) => {
  let isShowBtn = {
    allBtnDiv: false,
    clarifySaveBtn: false,
  };

  if (dataInfo?.id) {
    if (
      curRes?.['/purchaseSourceClarifyController/edit'] &&
      packListInfo?.state == CQXG_STATE() &&
      ((dataInfo?.state == `${getTenantID()}${ILoadMapTypeAll.PROCURECLARFY_STATE}1000` && !dataInfo?.checkState) ||
        (dataInfo?.state == `${getTenantID()}${ILoadMapTypeAll.PROCURECLARFY_STATE}1001` &&
          dataInfo?.checkState == SHTH_CHECKSTATE() &&
          dataInfo?.createrID == getUserID() &&
          dataInfo?.createPositionID == getPosIDs()))
    ) {
      isShowBtn['allBtnDiv'] = true;
      isShowBtn['clarifySaveBtn'] = true;
    }
  } else {
    if (curRes?.['/purchaseSourceClarifyController/add']) {
      isShowBtn['allBtnDiv'] = true;
      isShowBtn['clarifySaveBtn'] = true;
    }
  }

  return isShowBtn;
};

/** 报价单位按钮权限 */
const updateSupplierBtn = (curRes: Record<string, any>, dataInfo: Record<string, any>) => {
  let isShowBtn = {
    allBtnDiv: false,
    supplierSaveBtn: false,
  };

  if (dataInfo?.id) {
    if (
      (curRes?.['/purchaseSourceSupplierController/addSourceSupplier'] || curRes?.['/purchaseSourceSupplierController/editSourceSupplier']) &&
      dataInfo?.state == `${getTenantID()}${ILoadMapTypeAll.CURRYOUT_STATE}1006`
    ) {
      isShowBtn['allBtnDiv'] = true;
      isShowBtn['supplierSaveBtn'] = true;
    }
  }

  return isShowBtn;
};

/** 定标按钮权限 */
const updateCalibrateBtn = (curRes: Record<string, any>, dataInfo: Record<string, any>, supplierInfo: Record<string, any>) => {
  let isShowBtn = {
    allBtnDiv: false,
    calibrateSaveBtn: false,
  };
  // debugger
  // 长度不存在说明没有标包
  if (!supplierInfo?.length) {
    // isShowBtn['allBtnDiv'] = false;
    // isShowBtn['calibrateSaveBtn'] = false;
    return;
  }

  // 有的话就不展示暂存按钮
  const findRes = supplierInfo?.find((item) => {
    return item?.state != DB_STATE() && item?.state != LB_STATE() && item?.state != YDB_STATE();
  });

  if (findRes) {
    // isShowBtn['allBtnDiv'] = false;
    // isShowBtn['calibrateSaveBtn'] = false;
    message?.warn('还有标包未提交开评标结果，暂不可定标!');
    return;
  }

  // 都流标也不填了
  const flag = supplierInfo?.every((item) => item?.state == LB_STATE());
  if (flag) {
    // isShowBtn['allBtnDiv'] = false;
    // isShowBtn['calibrateSaveBtn'] = false;
    return;
  }

  if (dataInfo?.id && dataInfo?.state) {
    if (
      curRes?.['/purchaseSourceCalibrateController/editSourceCalibrate'] &&
      ((dataInfo?.state == `${getTenantID()}${ILoadMapTypeAll.CALIBRATE_STATE}1000` && !dataInfo?.checkState) ||
        (dataInfo?.state == `${getTenantID()}${ILoadMapTypeAll.CALIBRATE_STATE}1001` &&
          dataInfo?.checkState == SHTH_CHECKSTATE() &&
          dataInfo?.createrID == getUserID() &&
          dataInfo?.createPositionID == getPosIDs()))
    ) {
      isShowBtn['allBtnDiv'] = true;
      isShowBtn['calibrateSaveBtn'] = true;
    }
  } else {
    if (curRes?.['/purchaseSourceCalibrateController/addSourceCalibrate']) {
      isShowBtn['allBtnDiv'] = true;
      isShowBtn['calibrateSaveBtn'] = true;
    }
  }

  return isShowBtn;
};

export { AllBtnsConfig, updateInviteBtn, updateClarifyBtn, updateSupplierBtn, updateCalibrateBtn };
